package com.alkaid.anime.service.impl;

import com.alkaid.anime.mapper.AnimeMapper;
import com.alkaid.anime.mapper.AnimeVideoMapper;
import com.alkaid.anime.service.AnimeVideoService;
import com.alkaid.commons.msg.MsgDefinition;
import com.alkaid.commons.entity.*;
import com.alkaid.commons.ex.*;
import com.alkaid.commons.param.anime.AnimePlay;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Kylin
 */
@Service
public class AnimeVideoServiceImpl implements AnimeVideoService {

    private AnimeMapper animeMapper;
    private AnimeVideoMapper animeVideoMapper;

    @Autowired
    public void setAnimeMapper(AnimeMapper animeMapper) {
        this.animeMapper = animeMapper;
    }

    @Autowired
    public void setAnimeVideoMapper(AnimeVideoMapper animeVideoMapper) {
        this.animeVideoMapper = animeVideoMapper;
    }

    @Override
    public List<AnimeVideo> getAnimeVideos(Integer num) throws VideoNotFoundException {
        if (num == null) {
            throw new VideoNotFoundException("视频不存在");
        }
        List<AnimeVideo> animeVideos = animeVideoMapper.findAnimeVideos(num);
        if (animeVideos == null) {
            throw new VideoNotFoundException("视频不存在");
        }
        return animeVideos;
    }

    @Override
    public LinkedList<List<AnimeRefresh>> getAnimeRefreshLists() throws VideoNotFoundException {
        LinkedList<List<AnimeRefresh>> animeRefreshLists = new LinkedList<>();
        long now = System.currentTimeMillis();
        int start = -3;
        int end = 5;
        for (int i = start; i < end; i++) {
            long time = now + 24 * 60 * 60 * 1000L * i;
            List<AnimeRefresh> list = animeVideoMapper.findAnimeRefresh(new Date(time));
            animeRefreshLists.add(list);
        }
        boolean isNull = true;
        for (List<AnimeRefresh> item : animeRefreshLists) {
            if (item != null) {
                isNull = false;
                break;
            }
        }
        if (isNull) {
            throw new VideoNotFoundException("新番时间表未找到");
        }
        return animeRefreshLists;
    }

    @Override
    public AnimePlay getAnimePlay(Integer id) throws AnimeNotFoundException, VideoNotFoundException {
        if (id == null) {
            return null;
        }
        List<AnimeVideo> animeVideos = animeVideoMapper.findSameAnimeIdById(id);
        if (animeVideos.size() == 0) {
            throw new VideoNotFoundException("视频未找到");
        }
        Integer animeId = animeVideos.get(0).getAnimeId();
        Anime anime = animeMapper.selectById(animeId);
        if (anime == null) {
            throw new AnimeNotFoundException("番剧未找到");
        }
        return new AnimePlay(anime, animeVideos);
    }

    @Override
    public List<AnimeSource> getSource(String jwt, Long id) throws VideoNotFoundException, NotLoginException, IllegalException {
        AnimeVideo animeVideo = animeVideoMapper.findById(id);
        System.out.println(animeVideo);
        if (animeVideo == null) {
            throw new VideoNotFoundException("视频不存在");
        }
        Integer vipInt = 3;
        if (animeVideo.getAuth().equals(vipInt)) {
            // VIP会员视频
            if (jwt == null || "".equals(jwt)) {
                // jwt为空-请先登录
                throw new NotLoginException(MsgDefinition.LOGIN_PLEASE);
            }
            // 解析jwt
            JwtUtils jwtUtils = new JwtUtils();
            // 是否需要刷新
            boolean isRefresh = jwtUtils.parseJwt(jwt);
            if (isRefresh) {
                User user = jwtUtils.getUser();
                // 重新签发jwt
                // 更新token
                String token = jwtUtils.createJwt(user, true);
                throw new TokenRefreshException(token);
            }
            if (!jwtUtils.getUser().getAuthorities().toString().contains("VIP")) {
                // 非VIP，抛出权限不足异常
                throw new IllegalException(MsgDefinition.AUTHORITY_LOSS);
            }
        }
        List<AnimeSource> animeSources = animeVideoMapper.findSource(id);
        if (animeSources == null || animeSources.size() == 0) {
            throw new VideoNotFoundException("视频源不存在");
        }
        return animeSources;
    }
}
